﻿using System;
using System.Windows.Forms;
using GM.Model;
using System.Linq;

namespace GM.Forms
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void gbtnCancel_Click(object sender, EventArgs e)
        {
            if (LoginCancelled != null)
                LoginCancelled();
        }

        private void gbtnLogin_Click(object sender, EventArgs e)
        {
            var request = Pracownik.SingleOrDefault(x => x.login == tbxLogin.Text && x.haslo == tbxPassword.Text);

            if (LoginRequested != null)
                LoginRequested(request != null);
            ClearForm();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
            ClearForm();
        }

        private void ClearForm()
        {
            tbxLogin.Text = tbxPassword.Text = string.Empty;
        }

        public event Action<bool> LoginRequested;

        public event Action LoginCancelled;
    }
}
